//
//  CZEmoticonPageView.swift
//  Weibo27
//
//  Created by apple on 16/4/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
// cell 可重用标示符
private let CZEmoticonPageViewCellId = "CZEmoticonPageViewCellId"

class CZEmoticonPageView: UICollectionView {
    
    
    

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        self.backgroundColor = RandomColor()
        dataSource = self
        // 注册cell
        registerClass(CZEmoticonPictureViewCell.self, forCellWithReuseIdentifier: CZEmoticonPageViewCellId)
        
        // 设置layout
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        
        // 设置itemSize
        layout.itemSize = CGSize(width: SCREENW, height: 216 - 37)
        
        // 设置垂直和水平间距
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        // 更改我们pageView 的滚动方向
        layout.scrollDirection = .Horizontal
    }
    


}

extension CZEmoticonPageView: UICollectionViewDataSource{
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return CZEmoticonTools.sharedTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return CZEmoticonTools.sharedTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CZEmoticonPageViewCellId, forIndexPath: indexPath) as! CZEmoticonPictureViewCell
        cell.indexPath = indexPath
        cell.emoticons = CZEmoticonTools.sharedTools.allEmoticons[indexPath.section][indexPath.item]
        return cell
    }
}
